package view.overlay;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;

public class Button {
	private Rectangle mBounds;
	private MouseListener mListener;
	private String mText;
	
	private Color mBackgroundColor;
	private Color mHighlightedBackgroundColor;
	private Color mDepressedBackgroundColor;
	
	private Color mTextColor;
	private Color mHighlightedTextColor;
	private Color mDepressedTextColor;
	
	private boolean mIsHighlighted;
	private boolean mIsDepressed;
	
	public Button() {
		this("");
	}
	
	public Button(String text) {
		mText = text;
		
		// Set the default colors
		mBackgroundColor = new Color(80, 59, 25);
		mHighlightedBackgroundColor = new Color(167, 113, 28);
		mDepressedBackgroundColor = new Color(71, 46, 6);
	
		mTextColor = Color.white;
		mHighlightedTextColor = Color.black;
		mDepressedTextColor = Color.white;
	}
	
	/**
	 * Determines if this button contains the Point passed
	 * to it.
	 * @param p
	 * @return
	 */
	public boolean contains(Point p) {
		if (mBounds != null) {
			return (mBounds.contains(p));
		}
		return (false);
	}
	
	
	/**
	 * Overrideable drawing method
	 * @param g
	 */
	public void draw(Graphics g) {
		if (mBounds == null) return;
		
		if (mIsDepressed) {
			g.setColor(mDepressedBackgroundColor);
		}
		else if (mIsHighlighted) {
			g.setColor(mHighlightedBackgroundColor);
		}
		else {
			g.setColor(mBackgroundColor);
		}
		
		
		
		// Fill the border
		g.fill3DRect(mBounds.x, mBounds.y, mBounds.width, mBounds.height, true);
		
		// Draw the text
		Rectangle2D fontSize = g.getFontMetrics().getStringBounds(mText, g);
		
		if (mIsDepressed) {
			g.setColor(mDepressedTextColor);
		}
		else if (mIsHighlighted) {
			g.setColor(mHighlightedTextColor);
		}
		else {
			g.setColor(mTextColor);
		}
		
		Graphics2D g2 = (Graphics2D) g;
		
		Object prevHint = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		// actually draw the text
		g2.drawString(mText,
					(int)mBounds.getX() + mBounds.width / 2 - (int)fontSize.getWidth() / 2,
					(int)mBounds.getY() + mBounds.height / 2 + (int)fontSize.getHeight() / 2);
		
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, prevHint);
		
	}
	
	/**
	 * Sets the bounding rectangle for this Button
	 * @param r
	 */
	public void setBounds(Rectangle r) {
		mBounds = r;
	}
	
	public void setBounds(int x, int y, int width, int height) {
		if (mBounds == null) {
			mBounds = new Rectangle();
		}
		
		mBounds.setBounds(x, y, width, height);
	}
	
	/**
	 * Gets the bounding rectangle for this Button
	 * @return
	 */
	public Rectangle getBounds() {
		return (mBounds);
	}
	
	/**
	 * Activates the mouseClicked event on the
	 * MouseListener attached to this button.
	 * @param e
	 */
	public void activate(MouseEvent e) {
		if (mListener != null) {
			mListener.mouseClicked(e);
		}
	}
	
	/**
	 * Gets the text displayed on the button
	 * @return
	 */
	public String getText() {
		return (mText);
	}
	
	public void setText(String text) {
		mText = text;
	}
	
	/**
	 * Sets the MouseListener on this Button
	 * @param listener
	 */
	public void setListener(MouseListener listener) {
		mListener = listener;
	}
	
	public boolean isDepressed() {
		return (mIsDepressed);
	}
	
	public void setDepressed(boolean isDepressed) {
		mIsDepressed = isDepressed();
	}
	
	public boolean isHighlighted() {
		return (mIsHighlighted);
	}
	
	public void setHighlighted(boolean isHighlighted) {
		mIsHighlighted = isHighlighted;
	}
}
